﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace com.nm.library.Helper
{
   /// <summary>
   /// 
   /// </summary>
   public class ImageThumbnailHelper
    {

        /// <summary>
        ///使用方法调用GenerateHighThumbnail()方法即可
        /// </summary>
        /// <param name="image"></param>
        /// <param name="newImagePath"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        public static byte[] GenerateHighThumbnail(Image image, out int w,out int h)
        {
            const int width = 60;
            const int height = 40;
            var newWidth = AdjustSize(width, height, image.Width, image.Height).Width;
            var newHeight = AdjustSize(width, height, image.Width, image.Height).Height;
            //if (newWidth > newHeight) newWidth = newHeight;
            //if (newWidth < newHeight) newHeight = newWidth;
            w = newWidth;
            h = newHeight;
            var thumbnailImage = image.GetThumbnailImage(newWidth, newHeight, ThumbnailCallback, IntPtr.Zero);
            //转byte[]
            var ms = new MemoryStream();
            thumbnailImage.Save(ms, ImageFormat.Bmp);
            var bytes = ms.GetBuffer();
            ms.Close();
            thumbnailImage.Dispose();
            image.Dispose();
            return bytes;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static bool ThumbnailCallback()
        {
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mimeType"></param>
        /// <returns></returns>
        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            int j;
            var encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }


        /// <summary>
        /// 
        /// </summary>
        public struct PicSize
        {
            /// <summary>
            /// 
            /// </summary>
            public int Width;
            /// <summary>
            /// 
            /// </summary>
            public int Height;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="spcWidth"></param>
        /// <param name="spcHeight"></param>
        /// <param name="orgWidth"></param>
        /// <param name="orgHeight"></param>
        /// <returns></returns>
        public static PicSize AdjustSize(int spcWidth, int spcHeight, int orgWidth, int orgHeight)
        {
            var size = new PicSize();
            // 原始宽高在指定宽高范围内，不作任何处理 
            if (orgWidth <= spcWidth && orgHeight <= spcHeight)
            {
                size.Width = orgWidth;
                size.Height = orgHeight;
            }
            else
            {
                // 取得比例系数 
                var w = orgWidth / (float)spcWidth;
                var h = orgHeight / (float)spcHeight;
                // 宽度比大于高度比 
                if (w > h)
                {
                    size.Width = spcWidth;
                    size.Height = (int)(w >= 1 ? Math.Round(orgHeight / w) : Math.Round(orgHeight * w));
                }
                // 宽度比小于高度比 
                else if (w < h)
                {
                    size.Height = spcHeight;
                    size.Width = (int)(h >= 1 ? Math.Round(orgWidth / h) : Math.Round(orgWidth * h));
                }
                // 宽度比等于高度比 
                else
                {
                    size.Width = spcWidth;
                    size.Height = spcHeight;
                }
            }
            return size;
        }
    }
}
